package cn.amarone.scaffold.controller;

import cn.amarone.scaffold.config.business.common.BaseController;
import cn.amarone.scaffold.config.business.common.BaseReq;
import cn.amarone.scaffold.config.business.common.BaseReqPage;
import cn.amarone.scaffold.config.business.common.BaseRsp;
import cn.amarone.scaffold.entity.SysMenu;
import cn.amarone.scaffold.service.ISysMenuService;
import com.alibaba.fastjson2.JSON;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static cn.amarone.scaffold.entity.table.SysMenuTableDef.SYS_MENU;

/**
 * @author Amarone
 * @version 1.0
 * @description: 菜单管理
 * @date 2023/8/1 13:46
 */
@Tag(name = "菜单管理")
@RestController
@ApiSupport(author = "amarone")
@RequestMapping("/sysMenu")
@Slf4j
public class SysMenyController extends BaseController {

    @Autowired
    protected ISysMenuService sysMenuService;

    @Override
    @GetMapping("/queryByPage")
    public BaseRsp<Page<SysMenu>> queryByPage(@RequestBody BaseReqPage para) {
        return BaseRsp.success(sysMenuService.page(new Page<SysMenu>(para.getPageNumber(), para.getPageSize()), QueryWrapper.create().from(SYS_MENU)));
    }

    @GetMapping("/queryByLevel")
    public BaseRsp<List<SysMenu>> queryByLevel(@RequestBody BaseReqPage para) {
        return BaseRsp.success(sysMenuService.selectListWithRelationsByQuery(sysMenuService.queryChain().from(SYS_MENU).where(SYS_MENU.PARENT_ID.eq(0))));
    }

    @Override
    @GetMapping("/detail")
    public BaseRsp<SysMenu> queryDetail(@RequestBody BaseReq para) {
        SysMenu byId = sysMenuService.getById(1);
        JSON.toJSONString(byId);
        return BaseRsp.success(byId);
    }
}
